package servlet;

import utils.MyDBUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "SelectServlet")
public class SelectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Connection conn = MyDBUtil.connetDB(); // get db connection;
        PreparedStatement ps = null;
        ResultSet rs = null;

        String studentid = null;
        String quiz = null;
        String exam = null;

        String sql = "select * from grade where studentid = ? limit 1";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, "b63030007");
            rs = ps.executeQuery();
            while (rs.next()) {

                studentid = rs.getString("studentid");
                quiz = rs.getString("quiz");
                exam = rs.getString("exam");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String message = null;
        if(studentid == null){
            message = "Select FAIL";
        }else{
            message = "Select SUCCESS";
            request.setAttribute("studentid", studentid);
            request.setAttribute("quiz", quiz);
            request.setAttribute("exam", exam);
        }


        request.setAttribute("message", message);
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);

    }
}
